<?php
class TourController extends Zend_Controller_Action {
	// Mảng tham số nhận được ở mỗi Action:
	protected $_arrParam;
	private $USER_ID = 0;
	private $URI = "/";
	private $URL_ACC_LOGIN = "/account/login/";
	private $DEFAULT_PROFILE_PICTURE = "default/img/no_image.jpg";
	private $ALERT_TYPE_ERROR = "alert-error";
	private $ALERT_TYPE_SUCCESS = "alert-success";
	public function init() {
		$this->_arrParam = $this->_request->getParams ();
		// Truyền ra view
		$this->view->arrParam = $this->_arrParam;
		
		// Xu li cho phan header
		$this->URI = $this->getRequest ()->getScheme () . "://" . $this->getRequest ()->getHttpHost () . $this->getRequest ()->getRequestUri ();
		$mAccount = new Default_Model_Account ();
		if (! $mAccount->hasIdentity ()) {
			$this->_redirect ( $this->URL_ACC_LOGIN . '?return_url=' . $this->URI );
		} else {
			// get user info
			$this->USER_ID = $mAccount->getUserId ();
			
			$this->view->fullname = $mAccount->getFullName ();
			$this->view->return_url = $this->URI;
			$this->view->userid = $mAccount->getUserId ();
			$strAvatar = $mAccount->getProfilePicture ();
			if ($strAvatar != "") {
				$this->view->my_avatar = $mAccount->getProfilePicture ();
			} else {
				$this->view->my_avatar = $this->DEFAULT_PROFILE_PICTURE;
			}
			// get report
			$mRep = new Default_Model_Report();
			$arrRep = $mRep->listAllReport();
			$this->view->arrRep = $arrRep;
		}
		// End xu li cho phan header
	}
	public function indexAction() {
		$mUser = new Default_Model_User();
		$mUserTour = new Default_Model_UserTour();
		$mTourLoc = new Default_Model_TourLocation();
		$mLoc = new Default_Model_Location();
		$mTour = new Default_Model_Tour();
		$mPhoto = new Default_Model_Photo();
		
		if ($this->_request->isGet ()) {
			if (isset ( $_GET ['delete'] ) && $_GET ['delete'] == 1) {
				if (isset ( $_GET ['tour_id'] )) {
					// delete tour
					$mProcess = new Default_Model_YourTourProcess ();
					$strMessReturn = $mProcess->deleteTourInfo( $_GET ['tour_id'] );
					if ($strMessReturn == "") {
						$this->view->display_alert = 1;
						$this->view->alert_type = $this->ALERT_TYPE_SUCCESS;
						$this->view->alert_message = 'Xoá nhóm thành công!';
					} else {
						$this->view->display_alert = 1;
						$this->view->alert_type = $this->ALERT_TYPE_ERROR;
						$this->view->alert_message = '<strong>Error!</strong> ' . $strMessReturn;
					}
				} else {
					$this->view->display_alert = 1;
					$this->view->alert_type = "";
					$this->view->alert_message = '<strong>Warning!</strong> Thiếu thông tin để xoá.';
				}
			}
		}
		
		// process to display view
		$this->view->total_tour = $mTour->countTour();
		$this->view->my_user_id = $this->USER_ID;
		$arrTour = $mTour->listAllTour();
		if (count ( $arrTour ) != 0) {
			// paging
			$adapter = new Zend_Paginator_Adapter_DbSelect ( $arrTour );
			$paginator = new Zend_Paginator ( $adapter );
			$paginator->setItemCountPerPage ( 100 );
			$paginator->setPageRange ( 3 );
			$currentPage = $this->_request->getParam ( 'page', 1 );
			$paginator->setCurrentPageNumber ( $currentPage );
			$this->view->arrTour = $paginator;
			
			// get city and user
			$arrUser = null;
			$arrLoc = null;
			$iCountTour = 0;
			$arrPhoto = null;
			$arrStartLoc = null;
			$arrEndLoc = null;
			foreach ($paginator as $tour) {
				$arrUserTourInfo = $mUserTour->getUserCreateByTour($tour['tour_id']);
				if(count($arrUserTourInfo) != 0) {
					$arrUser[] = $mUser->getUserInfoById($arrUserTourInfo[0]['user_id']);
				}
				$arrLocInfo = $mTourLoc->getLocationByTourId($tour['tour_id']);
				// get location info
				if(count($arrLocInfo) != 0) {
					foreach ($arrLocInfo as $loc) {
						$arrLoc[$iCountTour][] = $mLoc->getLocationInfoById($loc['location_id']);
					}
				}
				// get photo
				$arrPhoto[] = $mPhoto->getPhotoByTourId($tour['tour_id']);
				
				$arrStartLoc[] = $mLoc->getLocationInfoById($tour['start_location']);
				$arrEndLoc[] = $mLoc->getLocationInfoById($tour['end_location']);
				
				$iCountTour++;
			}
			
			$this->view->arrUser = $arrUser;
			$this->view->arrLoc = $arrLoc;
			$this->view->arrPhoto = $arrPhoto;
			$this->view->arrStartLoc = $arrStartLoc;
			$this->view->arrEndLoc = $arrEndLoc;
		}
	}
	
}